#include<bits/stdc++.h>
#define N 105
#define int long long
using namespace std;
int T,n,a[200005],f[N][N];
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(0);
    cin>>T;
    while(T--){
        memset(f,-1,sizeof(f));
        cin>>n;
        int mx=0;
        for(int i=1;i<=n;i++) cin>>a[i],mx=max(mx,a[i]);
        a[n+1]=-1;
        if(n>100){
            int ans=0;
            for(int i=1;i<n;i++) if(a[i]==a[i+1]) ans+=a[i];
            cout<<ans<<'\n';
            continue;
        }
        int ss=0;
        for(int i=1;i<n;i++){
            f[i][0]=ss;
            if(a[i]==a[i-1]) ss+=a[i];
        }
        for(int i=1;i<=n;i++){
            for(int k=0;k<=mx;k++){
                if(f[i][k]==-1) continue;
                int sum=0;
                if(i+2<=n) sum=(a[i+2]==a[i+1])*a[i+1];
                for(int j=i+1;j<=n;j++){
                    if(i+1==j){
                        f[j][k]=max(f[j][k],f[i][k]+(a[i]==a[j])*a[i]);
                        sum+=(a[j]==k)*a[j];
                        continue;
                    }
                    if(i+2==j){
                        f[j][a[j-1]]=max(f[j][a[j-1]],f[i][k]+(a[j-1]==k)*k+(a[i]==a[j])*a[j]);
                        // cout<<i<<' '<<j<<' '<<a[i]<<' '<<a[j]<<endl;
                        continue;
                    }
                    f[j][a[j-1]]=max(f[j][a[j-1]],f[i][k]+(a[i]==a[j])*a[j]+sum);
                    if(j>i+1&&a[j]==a[j-1]) sum+=a[j];
                }
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            int ss=0;
            for(int j=i+2;j<=n;j++) if(a[j]==a[j-1]) ss+=a[j];
            for(int j=0;j<=mx;j++){
                if(f[i][j]==-1) continue;
                ans=max(ans,f[i][j]+ss+(j==a[i+1])*j);
            }
        }
        // cout<<f[6][5]<<' '<<f[14][5]<<endl;
        int tmp=0;
        for(int i=1;i<n;i++) if(a[i]==a[i+1]) tmp+=a[i];
        ans=max(ans,tmp);
        cout<<ans<<'\n';
    }
    return 0;
}
// g++ color.cpp -o exe -std=c++14 -O2 -Wall -Wextra -fsanitize=address,undefined

/*

3 5 1 5 2 1 2 4

1: 3, 6
2: 5, 7
3: 1
4: 8
5: 2, 4

dp[i][j] -> pos i with val=j, 

dp[i][j]=max {k=1,2...,i-1}{ dp[k][j] + same(k+1,i-1) }

-> O(N^2 M) =35pts

plus xielv youhua =50pts

*** ... buhui xielv 6

????? why do you need j ... sz

dp[i] -> pos i color it red max sum

dp[i]=dp[j]+same(j+1,i-1) ... a[i]==a[j]

<=O(n^2) 50~60pts

ooo a[i]==a[j] is not right

=O(N^2) =50pts

still wrong

dp[i][j]->pos i chosen(blue) and last red = j

dp[i][j]=dp[k][l]

nonono

35pts

17:05 100+100+35+0=235pts

*/